<template>
  <div class="login-container">
    <div auto-complete="on" class="login-form" label-position="left">
      <div class="title-container">
        <h3 class="title">快嘉脚手架后台</h3>

        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="密码登录" name="pass">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
              <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  auto-complete="on"
                  name="username"
                  placeholder="Username"
                  tabindex="1"
                  type="text"
                />
              </el-form-item>

              <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  auto-complete="on"
                  name="password"
                  placeholder="Password"
                  tabindex="2"
                  @keyup.enter.native="handleLogin"
                />
                <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
              </el-form-item>

              <el-button :loading="loading" style="width:100%;margin-bottom:30px;" type="primary"
                         @click.native.prevent="handleLogin">登录
              </el-button>

              <div class="tips">
                <span style="margin-right:20px;">username: admin</span>
                <span> password: 1234</span>
              </div>

            </el-form>
          </el-tab-pane>

          <el-tab-pane label="验证码登录" name="sms">
            <el-form ref="smsCodeForm" :model="smsCodeForm">
              <el-form-item prop="phone">
        <span class="svg-container">
          <svg-icon icon-class="message"/>
        </span>
                <el-input
                  ref="phone"
                  v-model="smsCodeForm.phone"
                  auto-complete="on"
                  name="phone"
                  placeholder="手机号"
                  tabindex="1"
                  type="text"
                />
              </el-form-item>

              <el-form-item prop="smsCode">
                <el-input
                  ref="smsCode"
                  v-model="smsCodeForm.smsCode"
                  auto-complete="on"
                  name="smsCode"
                  placeholder="验证码"
                  tabindex="2"
                  maxlength="6"
                  @keyup.enter.native="handleMobileLogin"
                />
                <span class="code-ver text-center" @click="getYzm"><i v-show="yzmTextLoading"
                                                                      class="el-icon-loading"></i>{{ yzmText }}</span>
              </el-form-item>

              <el-button :loading="loading" style="width:100%;margin-bottom:30px;" type="primary"
                         @click.native.prevent="handleMobileLogin">登录
              </el-button>

              <div class="tips">
                <span style="margin-right:20px;">phone: 13911111110</span>
                <span> smsCode: 发送验证码后，后台控制台查看</span>
              </div>

            </el-form>
          </el-tab-pane>
          <el-tab-pane label="微信扫码登录" name="wechat">
            <iframe id="mainIframe" name="mainIframe"
                    :src="href" style="height: 430px"></iframe>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>

<script>

import {validUsername} from '@/utils/validate'


import {smsCode, wxLogin} from '@/api/user'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error('The password can not be less than 4 digits'))
      } else {
        callback()
      }
    }
    return {
      activeName: 'pass',
      yzmText: "发送验证码",
      yzmTextLoading: false,

      href: process.env.VUE_APP_BASE_API+"/wechat/render",
      loginForm: {
        username: 'admin',
        password: '1234'
      },
      smsCodeForm: {
        phone: '13911111110',
        smsCode: ''
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  mounted() {
    this.getWeChatUrl()
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event);
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async getWeChatUrl() {
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({path: this.redirect || '/'})
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //获取验证码
    getYzm() {
      let reg = /^1\d{10}$/;
      if (!reg.test(this.smsCodeForm.phone)) {
        this.$message.warning("手机号码不符合规则");
        return;
      }
      if (this.yzmText == "发送验证码") {
        smsCode(this.smsCodeForm.phone).then(() => {
          this.$message.success("验证码发送成功");
        });

        this.yzmTextLoading = true;
        this.yzmText = "";
        setTimeout(() => {
          this.yzmTextLoading = false;
          var value = 60;
          this.yzmText = value + "s";
          this.yzmTimer = setInterval(() => {
            value--;
            this.yzmText = value + "s";
            if (value == 0) {
              this.yzmText = "发送验证码";
              clearInterval(this.yzmTimer);
              this.yzmTimer = null;
            }
          }, 1000);
        }, 1000);
      }
    },
    handleMobileLogin() {
      this.loading = true
      this.$store.dispatch('user/mobileLogin', this.smsCodeForm).then(() => {
        this.$router.push({path: this.redirect || '/'})
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

$background: no-repeat center center;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {

  .el-input {
    display: inline-block;
    height: 47px;
    width: 80%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #223a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #88ddff;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 60px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #000000;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .tab {
    width: 100%;
    height: 56px;
    line-height: 56px;
    display: -moz-flex;
    display: -ms-flex;
    display: -webkit-flex;
    display: flex;
    border-bottom: 2px solid #f2f5f9;

    div {
      margin: 0 28px;
      font-size: 14px;
      color: #9fa3b0;
      text-align: center;
      -moz-flex-grow: 1;
      -ms-flex-grow: 1;
      -webkit-flex-grow: 1;
      flex-grow: 1;
      cursor: pointer;
    }

    .active {
      font-size: 14px;
      color: #414a60;
      font-weight: bold;
      border-bottom: 3px solid #265bf6;
    }
  }

  .code-ver {
    width: 70px;
    font-size: 14px;
    color: #265bf6;
    border: none;
    background-color: white;
    cursor: pointer;
  }

  .text-center {
    text-align: center;
  }
}
</style>
